package com.duobaoyu.dby.supply.admin.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.resource.common.admin.dto.area.AreaListDTO;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceAddressAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.*;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.*;
import com.duobaoyu.dby.supply.core.common.exception.CustomAssert;
import com.duobaoyu.dby.supply.core.task.bean.exporttask.ExportParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 地址管理
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2022/6/6 9:29
 */
@Api("地址信息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/address")
public class AddressController {

	private final ResourceAddressAdminClient resourceAddressAdminClient;

	/**
	 * 地址查询
	 * @param addressQuery
	 * @return
	 */
	@GetMapping("/areas")
	@ApiOperation(value = "地址查询", notes = "地址查询")
	@SupplySysLog(value = "地址查询",operationType = OperationTypeEnum.SELECT)
	public R<List<AddressMappingVO>> getAreas(AddressQueryDTO addressQuery) {
		CustomAssert.isTrue(Objects.nonNull(addressQuery.getLevel())||Objects.nonNull(addressQuery.getAreaName())||Objects.nonNull(addressQuery.getAreaCode())||Objects.nonNull(addressQuery.getParentAreaCode()), "参数能全部为空");
		return R.ok(resourceAddressAdminClient.selectAreaList(addressQuery));
	}


	/**
	 * 查询地址库列表
	 * @param areaQueryList 查询条件
	 * @return R<Page<AreaListVO>>
	 */
	@GetMapping("/areas/list")
	@ApiOperation(value = "地址分页查询", notes = "地址分页查询")
	@SupplySysLog(value = "地址分页查询",operationType = OperationTypeEnum.SELECT)
	public R<Page<AreaListVO>> getAreaList(@Validated AreaQueryListDTO areaQueryList){

		return R.ok(resourceAddressAdminClient.areaPage(areaQueryList));
	}

	/**
	 * 查询三方地址
	 * @param channelAreas 三方地址查询
	 * @return R<Page<AreaVO>>
	 */
	@GetMapping("/channel/areas")
	@ApiOperation(value = "三方地址查询", notes = "三方地址查询")
	@SupplySysLog(value = "三方地址查询",operationType = OperationTypeEnum.SELECT)
	public R<List<AreaVO>> getChannelAreas(ChannelAreasDTO channelAreas){

		return R.ok(resourceAddressAdminClient.getChannelAreas(channelAreas));
	}

	/**
	 * 地址绑定
	 * @param addressBind
	 * @return R<Boolean>
	 */
	@PostMapping("/areas/bind")
	@ApiOperation(value = "渠道地址绑定", notes = "渠道地址绑定")
	@SupplySysLog(value = "渠道地址绑定",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> bindAreas(@RequestBody AreaBindDTO addressBind){

		return R.ok(resourceAddressAdminClient.bindAreas(addressBind));
	}

	/**
	 * 地址详情
	 * @param areasBindDetail 地址绑定参数
	 * @return
	 */
	@PostMapping("/areasBindDetail")
	@ApiOperation(value = "地址详情查询", notes = "地址详情查询")
	@SupplySysLog(value = "地址详情查询",operationType = OperationTypeEnum.SELECT)
	public R<List<AreasBindDetailVO>> areasBindDetail(@RequestBody @Validated AreasDetailDTO areasBindDetail){

		return R.ok(resourceAddressAdminClient.areasBindDetail(areasBindDetail));
	}

	/**
	 * 本地地址修改
	 * @param addressEdit
	 * @return
	 */
	@PostMapping("/supply-address/edit")
	@ApiOperation(value = "地址编码修改", notes = "地址编码修改")
	@SupplySysLog(value = "地址编码修改",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> updateSupplyAddress(@Validated @RequestBody AddressEditDTO addressEdit){

		return R.ok(resourceAddressAdminClient.updateSupplyAddressById(addressEdit));
	}

	/**
	 * 地图地址编码修改
	 * @param addressEdit
	 * @return
	 */
	@PostMapping("/mapAreaCode/edit")
	@ApiOperation(value = "地址地图编码修改", notes = "地址地图编码修改")
	@SupplySysLog(value = "地址地图编码修改",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> updateMapAreaCode(@Validated @RequestBody AddressEditDTO addressEdit){

		return R.ok(resourceAddressAdminClient.updateMapAreaCodeById(addressEdit));
	}

	/**
	 * 地址导出
	 * @param param
	 * @return
	 */
	@PostMapping("/supply-address/export")
	@ApiOperation(value = "地址导出", notes = "地址导出")
	@SupplySysLog(value = "地址导出",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> addressLibraryExport(@Validated @RequestBody ExportParam<AreaListDTO> param){

		return R.ok(resourceAddressAdminClient.addressLibraryExport(param));
	}

	/**
	 * 查询渠道三方地址
	 * @param channelAddressQuery 三方地址查询
	 * @return R<Page<ChannelAddressVO>>
	 */
	@GetMapping("/channel/areas/list")
	@ApiOperation(value = "查询渠道三方地址", notes = "查询渠道三方地址")
	@SupplySysLog(value = "查询渠道三方地址",operationType = OperationTypeEnum.SELECT)
	public R<Page<ChannelAddressVO>> getChannelAreas(@Validated ChannelAddressQueryDTO channelAddressQuery){

		return R.ok(resourceAddressAdminClient.channelAddressPage(channelAddressQuery));
	}
}
